package com.example.mvp.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.baselibrary.R;
import com.example.mvp.adapter.BottomChoseAdapter;
import com.example.mvp.bean.BottomChoseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 底部选择弹出框
 *
 * @author chaohaipeng  on 2018/8/17.
 */

public class BottomChoseDialog extends WindowBottomDialog {

    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 数据
     */
    private List<BottomChoseEntity> data;
    /**
     * 列表
     */
    private ListView lv_lbcd_list;
    /**
     * 标题
     */
    private TextView tv_dailog_bottom_title_text;

    /**
     * 适配器
     */
    private BottomChoseAdapter bottomChoseAdapter;
    /**
     * 接口点击返回
     */
    private DialogEventInterface eventInterface;
    private View bottomView;

    public BottomChoseDialog(@NonNull Context context) {
        super(context);
        mContext = context;
        initBottomDialogs();
    }

    public BottomChoseDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
        initBottomDialogs();
    }

    protected BottomChoseDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mContext = context;
        initBottomDialogs();
    }

    /**
     * 初始化底部弹出视图
     */
    private void initBottomDialogs() {
        data = new ArrayList<>();
        bottomView = View.inflate(mContext, R.layout.layout_bottom_chose_dialog, null);
        lv_lbcd_list = bottomView.findViewById(R.id.lv_lbcd_list);
        tv_dailog_bottom_title_text = bottomView.findViewById(R.id.tv_dailog_bottom_title_text);
        bottomChoseAdapter = new BottomChoseAdapter(mContext, data);
        lv_lbcd_list.setAdapter(bottomChoseAdapter);
        TextView tv_dailog_cbottom_cancel_text = bottomView.findViewById(R.id.tv_dailog_cbottom_cancel_text);
        tv_dailog_cbottom_cancel_text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        lv_lbcd_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if (eventInterface != null) {
                    dismiss();
                    eventInterface.OnItemClick(i);
                }
            }
        });

    }

    /**
     * 设置标题
     */
    public void setTitle(String title) {
        if (tv_dailog_bottom_title_text != null) {
            tv_dailog_bottom_title_text.setText(title);
        }

    }

    /**
     * 设置数据
     *
     * @param datas
     */
    public void setData(List<BottomChoseEntity> datas) {
        if (data == null || bottomChoseAdapter == null) {
            return;
        }
        data.clear();
        data.addAll(datas);
        bottomChoseAdapter.notifyDataSetChanged();
        setBottomView(bottomView);
    }

    public void setEventInterface(DialogEventInterface event) {
        this.eventInterface = event;
    }

    /**
     * 布局控件事件接口
     */
    public interface DialogEventInterface {
        /**
         * 点击数据
         */
        public void OnItemClick(int position);
    }


}
